//
//  SearchCollectionViewController.m
//  InSuger
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 Dior_3g. All rights reserved.
//

#import "SearchCollectionViewController.h"
#import "Header.h"
#import "CollectionViewCell.h"
#import "SearchIcon.h"
#import "SearchDetailsViewController.h"
#import "IconDetailsViewController.h"
@interface SearchCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UISearchBarDelegate>
@property(nonatomic, strong) UISearchBar *searchBar;
@property(nonatomic, strong) NSMutableArray *searchArr;
@property(nonatomic, strong) UICollectionView *collection;
@property(nonatomic, strong)UICollectionViewFlowLayout *flowLayout;
@property(nonatomic, strong) MBProgressHUD *progress;
@end

@implementation SearchCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
//    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
//    self.navigationItem.title = @"搜索";
//    self.navigationController.navigationBar.translucent = NO;
//    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    
    [self createSearchBar];
    [self createCollection];
    
    self.progress = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _progress.labelText = @"Loading...";
    _progress.mode = MBProgressHUDModeDeterminate;

    self.searchArr = [NSMutableArray array];
    [self createData];
    
    [self createTapGestureRecognizer]; 
}
#pragma mark 回收键盘
- (void)createTapGestureRecognizer{
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGestureRecognizer.cancelsTouchesInView = NO;
    //将触摸事件添加到当前view
    [self.view addGestureRecognizer:tapGestureRecognizer];
}
-(void)keyboardHide:(UITapGestureRecognizer*)tap{
    [self.searchBar resignFirstResponder];
}





#pragma mark SearchBar Setting
- (void)createSearchBar
{
    self.searchBar = [[UISearchBar alloc] init];
    self.searchBar.placeholder = @"搜索单品";
    self.searchBar.translucent = NO;
    [self.view addSubview:self.searchBar];
    self.searchBar.delegate = self;
    
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).with.offset(5);
        make.right.equalTo(self.view.mas_right).with.offset(-5);
        make.top.equalTo(self.view.mas_top).with.offset(1);
        make.height.mas_equalTo(35);
    }];
    
}
// 键盘中，搜索按钮被按下，执行的方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    SearchDetailsViewController *searchDetailsVC = [[SearchDetailsViewController alloc] init];
    searchDetailsVC.searchStr = searchBar.text;
    searchDetailsVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:searchDetailsVC animated:YES];
    [self.searchBar resignFirstResponder];
    searchBar.text = @"";
    
}



#pragma mark CollectionView Setting
- (void)createCollection
{
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake(0.5 * self.view.frame.size.width, 0.10625937 * self.view.frame.size.height);
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 2;
    
    self.collection = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:self.flowLayout];
    self.collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collection];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).with.offset(0);
        make.left.equalTo(self.view.mas_left).with.offset(0);
        make.top.equalTo(self.view.mas_top).with.offset(36);
        make.height.equalTo(self.view);
    }];
    
    @weakify(self);
    [self addColorChangedBlock:^{
        @strongify(self);
        self.collection.nightBackgroundColor = UIColorFromRGB(0x343434);
        self.collection.normalBackgroundColor = [UIColor whiteColor];
        self.navigationController.navigationBar.nightBarTintColor = UIColorFromRGB(0x444444);
        self.navigationController.navigationBar.normalBarTintColor = [UIColor redColor];
        self.view.nightBackgroundColor = UIColorFromRGB(0x343434);
        self.view.normalBackgroundColor = [UIColor whiteColor];
        self.searchBar.nightBackgroundColor = UIColorFromRGB(0x343434);

    }];
    self.navigationItem.title = @"搜索";
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.collection registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"collectionReuse"];

}
#pragma mark CollectionView Delegate Method
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.searchArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
        CollectionViewCell *cell = [self.collection dequeueReusableCellWithReuseIdentifier:@"collectionReuse" forIndexPath:indexPath];
        [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:(NSString *)[self.searchArr[indexPath.row] icon]] placeholderImage:[UIImage imageNamed:@"PlaceHolder.png"]];
        cell.headLabel.text = [self.searchArr[indexPath.row] name];
        cell.footLabel.text = [self.searchArr[indexPath.row] en_name];
        
        return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    IconDetailsViewController *IconDetailsVC = [[IconDetailsViewController alloc] init];
    IconDetailsVC.hidesBottomBarWhenPushed = YES;
    IconDetailsVC.category = [self.searchArr[indexPath.row] iconId];
    IconDetailsVC.iconTitle = [self.searchArr[indexPath.row] name];
    
    [self.navigationController pushViewController:IconDetailsVC animated:YES];
}

#pragma mark Internet Data
- (void)createData
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"client_secret"] = @"9c1e6634ce1c5098e056628cd66a17a5";
    dic[@"client_id"] = @"bt_app_ios";
    
    [AFN postdataWithString:@"http://open3.bantangapp.com/category/list" bodyStr:dic block:^(id block) {
        NSDictionary *dic = block;
        self.searchArr = [NSMutableArray array];
        self.searchArr = [SearchIcon baseModelByArr:dic[@"data"]];
        self.progress.hidden = YES;
        [self.collection reloadData];
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
